package com.example.xianji.activity;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.xianji.Contant;
import com.example.xianji.R;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Main_MyRengZhengActivity extends Activity {
    //    private SharedPreferences sp;
//    private Editor ed;
    private Button liji_renzheng;
    private TextView renzheng_delect;
    private ImageView imageview_shengfenzheng;
    private RelativeLayout my_renzheng_back;
    private TextView renzheng_zhuangtai, renzheng_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__my_reng_zheng);

//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();
        liji_renzheng = (Button) findViewById(R.id.liji_renzheng);
        renzheng_delect = (TextView) findViewById(R.id.renzheng_delect);
        my_renzheng_back = (RelativeLayout) findViewById(R.id.my_renzheng_relative_back);
        renzheng_zhuangtai = (TextView) findViewById(R.id.renzheng_zhuangtai);
        imageview_shengfenzheng = (ImageView) findViewById(R.id.imageview_shengfenzheng);
        renzheng_title = (TextView) findViewById(R.id.renzheng_title);

        if (PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TYPE, "").equals("FC") ||
                PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TYPE, "").equals("AC")) {
            renzheng_title.setText("营业执照");

        } else {
            renzheng_title.setText("身份证");

        }
        get_renzheng_data();

        liji_renzheng.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(Main_MyRengZhengActivity.this, Main_GeRen_RenZhengActivity.class));
            }
        });

        renzheng_delect.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                delect_renzheng();
            }
        });

        my_renzheng_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Main_MyRengZhengActivity.this.setResult(Contant.back_resultCode);
                Main_MyRengZhengActivity.this.finish();
            }
        });
    }

    /**
     * 请求认证结果
     */
    public void get_renzheng_data() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        parms.addBodyParameter("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        httpUtils.send(HttpMethod.POST, Contant.huoqu_renzheng, parms, new RequestCallBack<String>() {
            @Override
            public void onFailure(HttpException arg0, String arg1) {
            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                JSONObject object;
                try {
                    object = new JSONObject(arg0.result);
                    Toast.makeText(Main_MyRengZhengActivity.this, object.getString("msg"), Toast.LENGTH_SHORT).show();
                    if (object.getInt("status") == 1) {
                        JSONObject ob = object.getJSONObject("data");
                        String path = ob.getString("path");
                        String name = ob.getString("name");
                        String authen = ob.getString("authen");
                        if (authen.equals("A")) {
                            renzheng_zhuangtai.setText("认证状态 : 申请中");
                        } else if (authen.equals("Y")) {

                            renzheng_zhuangtai.setText("认证状态 : 认证通过");
                        } else if (authen.equals("N")) {
                            renzheng_zhuangtai.setText("认证状态 : 未通过");
                        } else if (authen.equals("D")) {
                            renzheng_zhuangtai.setText("认证状态 : 默认");
                        }

                        ImageLoader.getInstance().displayImage(Contant.chakan_renzheng + path + name, imageview_shengfenzheng,
                                new SimpleImageLoadingListener() {
                                    @Override
                                    public void onLoadingComplete(String imageUri,
                                                                  View view, Bitmap loadedImage) {
                                        ImageView imageView = (ImageView) view;
                                        imageView.setImageBitmap(loadedImage);
                                    }
                                });
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 删除认证
     */

    public void delect_renzheng() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        parms.addBodyParameter("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        httpUtils.send(HttpMethod.POST, Contant.delect_renzheng, parms, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                JSONObject object;
                try {
                    object = new JSONObject(arg0.result);
                    Toast.makeText(Main_MyRengZhengActivity.this, object.getString("msg"), Toast.LENGTH_SHORT).show();
                    if (object.getInt("status") == 1) {
                        Main_MyRengZhengActivity.this.finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (Contant.renzheng_success == 1) {
            get_renzheng_data();
            Contant.renzheng_success = 0;
        }
    }
}